<template>
  <div class="app-container">
    <div class="filter-tools">
      <el-form :inline="true" :model="filter" class="filter-form">
        <el-form-item label="执行状态">
          <el-select
            v-model="filter.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option v-for="(data,i) in status" :key="i" :label="data" :value="i"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input
            v-model="filter.title"
            placeholder="标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="测试产品/模块">
          <el-input
            v-model="filter.explain"
            placeholder="产品/模块"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="说明">
          <el-input
            v-model="filter.remark"
            placeholder="说明"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input
            v-model="filter.created_adminer"
            placeholder="创建人"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="执行人">
          <el-input
            v-model="filter.test_adminer"
            placeholder="执行人"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="唯一标识">
          <el-input
            v-model="filter.test_report_uid"
            placeholder="测试单唯一标识"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="ID">
          <el-input
            v-model="filter.id"
            placeholder="ID"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList()">
            筛选
          </el-button>
          <el-button type="primary" icon="el-icon-sugar" plain
                     @click="runReprotDialog()"
          >
            执行测试单
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table ref="singleTable" v-loading="dataLoading" :data="dataList" :height="height" border highlight-current-row>
        <el-table-column label="测试单唯一标识UUID" width="120">
          <template slot-scope="scope">
            <div>{{scope.row.test_report_uid}}</div>
            <el-radio @change="handleCurrentChange(scope.row)" :label="scope.row.test_report_uid" v-model="test_report_uid">{{""}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="测试单标题"
          align="center"
        />
        <el-table-column label="测试模块汇总" min-width="150">
          <template slot-scope="scope">
            <p
              style="margin-bottom:5px"
              v-for="(item,i) in scope.row.explain"
              :key="item"
              effect="dark">
              {{i + 1 + "."}}{{ item }}
            </p>

            <el-button @click="goTestModule(scope.row)">查看模块报告</el-button>
          </template>
        </el-table-column>
        <el-table-column label="数据" width="220">
          <template slot-scope="scope">
            <div  class="goto" @click="goTestApi(scope.row)"><b>总共测试接口数:</b>{{scope.row.test_api_sun}}</div>
            <div  class="goto" @click="goTestParam(scope.row)"><b>总共执行用例数:</b>{{scope.row.test_param_sun}}</div>
            <div class="goto" @click="goTestParam(scope.row)"><b>测试成功通过用例数:</b>{{scope.row.success_param_sun}}</div>
            <div  class="goto" @click="goTestParam(scope.row)"><b>总共执行验证器:</b>{{scope.row.test_validator_sum}}</div>
            <div class="goto" @click="goTestParam(scope.row)"><b>成功通过验证器:</b>{{scope.row.success_validator_sum}}</div>
            <div><b>用例总共执行用时:</b>{{scope.row.duration}}毫秒</div>
            <div><b>通过率:</b>{{scope.row.passing_rate + "%"}}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <div><el-tag :type="scope.row.status === 1 ? 'success' : 'info'">{{status[scope.row.status]}}</el-tag></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          label="测试单说明"
          align="center"
        />
        <el-table-column label="时间/人" min-width="200">
          <template slot-scope="scope">
            <div><b>测试单创建时间:</b>{{scope.row.created_at}}</div>
            <div><b>测试单创建人:</b>{{scope.row.created_adminer}}</div>
            <div><b>执行测试时间:</b>{{scope.row.tested_at}}</div>
            <div><b>执行测试人:</b>{{scope.row.test_adminer}}</div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="filter.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="filter.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="filter.total"
        @size-change="handleSizeChange"
        @current-change="getList"
      ></el-pagination>
    </div>

    <el-dialog title="执行测试单" :visible.sync="runReprotStatus" width="800px">
      <el-form label-width="80px">
        <el-form-item label="唯一标识" >
          <el-input :disabled="true" :placeholder="test_report_uid" class="filter-item"/>
        </el-form-item>
        <el-form-item label="标题" >
          <el-input :disabled="true" :placeholder="title" class="filter-item"/>
        </el-form-item>
        <el-form-item label="接口总数" >
          <el-input :disabled="true" :placeholder="test_api_sun" class="filter-item"/>
        </el-form-item>
        <el-form-item label="用例总数" >
          <el-input :disabled="true" :placeholder="test_param_sun" class="filter-item"/>
        </el-form-item>
        <el-form-item label="测试模块" >
          <el-tag
            style="margin-bottom:5px"
            v-for="item in explain"
            :key="item"
            :type="item"
            effect="dark">
            {{ item }}
          </el-tag>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="runReprotStatus = false">取消</el-button>
        <el-button type="primary" @click="handleRunReprot()">确认执行测试</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/runTest/testReportApi' // API接口
import {runTest} from '@/api/runTest/runTestApi' // API接口

export default {
  name: 'testModules',
  data() {
    return {
      height:this.$baseTableHeight(1),
      status:{
        null:'全部',
        0:'未执行测试',
        1:'已执行测试'
      },
      filter: {
        id:"",
        test_report_uid:"",
        page: 1,
        pageSize: 10,
        total: 0,
        created_adminer:"",
        test_adminer:"",
        title: '',
        remark:"",
        explain:"",
        status: "null",
      },
      dataLoading: true,
      dataList: [],
      dialogStatus:false,
      topStatus:0,
      inputVisible: false,
      inputValue: '',
      test_report_uid:"",
      runReprotStatus:false,
      title:"",
      explain:[],
      test_api_sun:0,
      test_param_sun:0,
    }
  },
  created() {
    this.filter.id = this.$route.query.host_env_id || null
    this.filter.test_report_uid = this.$route.query.test_report_uid || null
    this.getList()
  },
  methods: {
    getList(page = 1) {
      this.filter.page = Number(page)
      api.list(this.filter).then((res) => {
        this.dataList = res.data.list
        this.filter.total = res.data.total
        this.dataLoading = false
      })
    },
    handleSizeChange(val) {
      this.filter.pageSize = val
      this.getList(1)
    },
    runReprotDialog(){
      if(!this.test_report_uid) {
        this.$message.error('请先选择一个需要执行的测试单')
        return false
      }
      this.runReprotStatus = true
    },
    handleCurrentChange(data) {
      this.test_report_uid = data.test_report_uid
      this.test_api_sun = data.test_api_sun
      this.test_param_sun = data.test_param_sun
      this.title = data.title
      this.explain = data.explain
    },
    handleRunReprot() {
      if(!this.test_report_uid) {
        this.$message.error('请先选择一个需要执行的测试单')
        return false
      }
      runTest({test_report_uid:this.test_report_uid}).then((res) => {
        this.getList()
        this.test_report_uid = ""
        this.title = ""
        this.explain = ""
        this.runReprotStatus = false
      })
    },
    goTestModule(data) {
      this.$router.push({path:'/runTest/testModules',query:{test_report_uid:data.test_report_uid}});
    },
    goTestApi(data) {
      this.$router.push({path:'/runTest/testApis',query:{test_report_uid:data.test_report_uid}});
    },
    goTestParam(data) {
      this.$router.push({path:'/runTest/testParamRes',query:{test_report_uid:data.test_report_uid}});
    },

  },
}
</script>

<style>
.avatar-uploader .el-upload{
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
</style>
<style lang="scss" scoped>
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.goto:hover{color: #0051ff;}
.goto{
  cursor:pointer
}
</style>


